package com.mate.cloud.canal.client;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 事件派发，在项目启动之后派发消息启动CanalService里面onmessage方法
 *
 * @email: 448341911@qq.com
 * @author: MI
 * @createDate:
 * @updateUser: MI
 * @updateDate:
 * @updateRemark: 修改内容
 * @version: 1.0
 */
@Slf4j
public class CanalSpringListener extends ContextRefreshedEvent implements ApplicationContextAware, ApplicationRunner {


    private static ApplicationContext applicationContext;

    public CanalSpringListener(ApplicationContext source) {
        super(source);
    }

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("项目启动成功，开始派发CanalSpringListener事件");
        CanalSpringListener.applicationContext.publishEvent(this);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CanalSpringListener.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return CanalSpringListener.applicationContext.getBean(clazz);
    }

}
